package com.d3works.apps.base.service;

import com.d3works.apps.base.db.Partner;
import com.d3works.apps.base.db.Product;
import com.d3works.apps.tool.service.TranslationService;
import com.google.inject.Inject;
import java.util.HashMap;
import java.util.Map;

public class InternationalServiceImpl implements InternationalService {

  protected TranslationService translationService;

  @Inject
  public InternationalServiceImpl(TranslationService translationService) {
    this.translationService = translationService;
  }

  @Override
  public String translate(String source, String sourceLanguage, String targetLanguage) {
    String translationKey = translationService.getTranslationKey(source, sourceLanguage);
    return translationService.getValueTranslation(translationKey, targetLanguage);
  }

  @Override
  public Map<String, String> getProductDescriptionAndNameTranslation(
      Product product, Partner partner, String userLanguage) {
    Map<String, String> translation = new HashMap<>();
    if (product != null && partner != null) {
      String partnerLanguage = partner.getLanguage().getCode();
      translation.put(
          "description", translate(product.getDescription(), userLanguage, partnerLanguage));
      translation.put("productName", translate(product.getName(), userLanguage, partnerLanguage));
    }
    return translation;
  }
}
